package com.demo.controller.dataManagemant;

import com.demo.common.ReturnCode;
import com.demo.common.model.Object;
import com.demo.common.model.ObjectPoet;
import com.demo.service.dataManagement.PoetService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.Date;
import java.util.List;

/**
 * @Classname Poet
 * @Description 诗人
 * @Date 2020/3/10 18:38
 * @Created by yuanhang
 */
public class PoetController extends Controller {
    @Inject
    PoetService poetService;

    public void index() {
        set("router", "/dataManagement/poet");
        render("index.html");
    }

    /**
     * @return
     * @throws
     * @description 条件查询
     * @author yuanhang
     * @date 2020/3/10 19:02
     * @Param
     */
    public void condition(String name, int page, int limit) {
        Ret ret = new Ret();
        Page<Record> pageRecord = poetService.condition(name, page, limit);
        ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg()).set("data", pageRecord.getList()).set("count", pageRecord.getTotalRow());
        renderJson(ret);
    }


    /**
     * @return
     * @throws
     * @description 分页查询
     * @author yuanhang
     * @date 2020/3/10 19:02
     * @Param
     */
    public void paginate(int page, int limit) {
        Ret ret = new Ret();
        Page<Record> pageRecord = poetService.condition("", page, limit);
        ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg()).set("data", pageRecord.getList()).set("count", pageRecord.getTotalRow());
        renderJson(ret);
    }

    /**
     * @return
     * @throws
     * @description
     * @author yuanhang
     * @date 2020/3/11 12:28
     * @Param
     */
    public void detail(int id) {
        List<Record> recordList = Db.template("poet.id", id).find();
        if(recordList.size() > 0 ){
            set("poet", recordList.get(0));
        }else{
            set("poet", new ObjectPoet());
        }
        render("detail.html");
    }

    /**
     * @return
     * @throws
     * @description
     * @author yuanhang
     * @date 2020/3/11 12:28
     * @Param
     */
    public void edit(int id) {
        List<Record> recordList = Db.template("poet.id", id).find();
        if(recordList.size() > 0 ){
            set("poet", recordList.get(0));
        }else{
            set("poet", new ObjectPoet());
        }
        render("edit.html");
    }

    /**
     * @return
     * @throws
     * @description 新增
     * @author yuanhang
     * @date 2020/3/10 19:02
     * @Param
     */
    public void add(@Para("") ObjectPoet poet) {
        Ret ret = new Ret();
        poet.setCreateTime(new Date());
        poet.setLastUpdateTime(new Date());
        if (poet.save()) {
            ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg());
        } else {
            ret.set("code", ReturnCode.ERROR.getCode()).set("msg", ReturnCode.ERROR.getMsg());
        }
        renderJson(ret);
    }

    /**
     * @return
     * @throws
     * @description 更新
     * @author yuanhang
     * @date 2020/3/10 19:01
     * @Param
     */
    public void update(@Para("") ObjectPoet poet) {
        Ret ret = new Ret();
        poet.setLastUpdateTime(new Date());
        if (poet.update()) {
            ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg());
        } else {
            ret.set("code", ReturnCode.ERROR.getCode()).set("msg", ReturnCode.ERROR.getMsg());
        }
        renderJson(ret);
    }

    /**
     * @return
     * @throws
     * @description 删除
     * @author yuanhang
     * @date 2020/3/10 19:01
     * @Param
     */
    public void delete(int id) {
        Ret ret = new Ret();
        ObjectPoet poet = new ObjectPoet();
        poet.setId(id);
        if (poet.delete()) {
            ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg());
        } else {
            ret.set("code", ReturnCode.ERROR.getCode()).set("msg", ReturnCode.ERROR.getMsg());
        }
        renderJson(ret);
    }
}
